/**
 * 2018年11月27日下午1:35:54
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.OnLineDao;

import redis.clients.jedis.Jedis;

@Service
public class OnLineService extends BaseService {
	@Autowired
	private OnLineDao dao;
	private Jedis jedis = RedisKit.getJedis();
	public String getOnLineInfo() {
		//管理客户人数
		int UserCountA = dao.queryForInt("select count(*) from t_user where D_Source='1'");
		//已登录过的人
		int LoginCountA = dao.queryForInt("select count(*) from t_user where D_Source='1' and D_lastLoginTime!=''");
		//未登录过的人
		int UnLoginCountA = UserCountA-LoginCountA;
		//管理在线人数
		List<String> users= dao.queryForListByString("select D_Tel from t_user  where D_Source='1'");
		int count=0;
		for(String tel:users) {
			if(jedis.sismember("zaixian", tel)) {
				count++;
			}
		}		
		int OnlineCountA = count;
		//管理已收藏数
		List<String> glYsc = dao.queryForListByString("select UserName from t_collection where UserName in (select D_Tel from t_user where D_Source='1') GROUP BY UserName");
		int CollectionUserCountA = glYsc.size();
		/************认筹用户*********************/
		int UserCountB= dao.queryForInt("select count(*) from t_user where D_Source='0'");
		int LoginCountB =dao.queryForInt("select count(*) from t_user where D_Source='0' and D_lastLoginTime!='0'");
		int UnLoginCountB = UserCountB-LoginCountB;
		Long OnlineCountB = jedis.scard("zaixian") - OnlineCountA;
		List<String> rcYsc = dao.queryForListByString("select UserName from t_collection where UserName in (select D_Tel from t_user where D_Source='0') GROUP BY UserName");
		int CollectionUserCountB = rcYsc.size();
		/**********************************/
		//总房源数
		int RoomCount = dao.queryForInt("select count(*) from room");
		//总销售数 包括 用户真实购买量加上内控翻红的房源
		int SaleRoomCount = dao.queryForInt("select count(*) from room where D_state=2 or D_Monitor=3");
		//内控 只计算内控状态为1的房源
		int MonitorSaleRoomCount = dao.queryForInt("select count(*) from room where D_Monitor=1");
		//销售 用户真实购买量
		int UnMonitorSaleRoomCount = dao.queryForInt("select count(*) from room where D_state=2");
		//剩余
		int UnSaleRoomCount = RoomCount-SaleRoomCount;
		//销售率
		double SaleRoomProportion = SaleRoomCount/RoomCount*100;
		/**********收藏******************/
		//真实收藏不为0的房源
		int CollectionRoomCount = dao.queryForInt("select count(*) from room where E_CollectionCount!=0");
		/*************总额******************/
		int Total = dao.queryForInt("select sum(D_Total) from room where D_state=2 or D_Monitor=3");
		
		Map<String,Object> onLineMap = new HashMap<String,Object>();
		onLineMap.put("UserCountA", UserCountA);
		onLineMap.put("LoginCountA", LoginCountA);
		onLineMap.put("UnLoginCountA", UnLoginCountA);
		onLineMap.put("OnlineCountA", OnlineCountA);
		onLineMap.put("CollectionUserCountA", CollectionUserCountA);
		onLineMap.put("UserCountB", UserCountB);
		onLineMap.put("LoginCountB", LoginCountB);
		onLineMap.put("UnLoginCountB", UnLoginCountB);
		onLineMap.put("OnlineCountB", OnlineCountB);
		onLineMap.put("CollectionUserCountB", CollectionUserCountB);
		onLineMap.put("RoomCount", RoomCount);
		onLineMap.put("SaleRoomCount", SaleRoomCount);
		onLineMap.put("MonitorSaleRoomCount", MonitorSaleRoomCount);
		onLineMap.put("UnMonitorSaleRoomCount", UnMonitorSaleRoomCount);
		onLineMap.put("UnSaleRoomCount", UnSaleRoomCount);
		onLineMap.put("SaleRoomProportion", SaleRoomProportion);
		onLineMap.put("CollectionRoomCount", CollectionRoomCount);
		onLineMap.put("Total", Total);
		
		return JsonKit.toJson(onLineMap);
	}
}
